package com.zhr.business.sys.service.impl;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhr.business.sys.domain.SysGoods;
import com.zhr.business.sys.domain.SysUser;
import com.zhr.business.sys.mapper.SysGoodsMapper;
import com.zhr.business.sys.service.SysGoodsService;
import com.zhr.business.sys.vo.SysGoodsVO;
import com.zhr.common.api.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class SysGoodsServiceImpl extends ServiceImpl<SysGoodsMapper,SysGoods> implements SysGoodsService{

    @Override
    public IPage<SysGoodsVO> getList(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("size")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("current")));
        LambdaQueryWrapper<SysGoods> wrapper = createWrapper(params);

        // 执行分页查询
        IPage<SysGoods> sysGoodsPage = page(new Page<>(pageNum, pageSize), wrapper);

        // 将查询结果中的SysGoods对象转换为SysGoodsVO对象
        List<SysGoodsVO> sysGoodsVOList = sysGoodsPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());

        // 创建新的IPage对象，其中包含转换后的SysGoodsVO对象列表
        IPage<SysGoodsVO> sysGoodsVOPage = new Page<>();
        sysGoodsVOPage.setRecords(sysGoodsVOList);
        sysGoodsVOPage.setCurrent(sysGoodsPage.getCurrent());
        sysGoodsVOPage.setSize(sysGoodsPage.getSize());
        sysGoodsVOPage.setTotal(sysGoodsPage.getTotal());

        return sysGoodsVOPage;
    }

    @Override
    public Result<String> updateGood(SysGoods sysGoods) {

        return null;
    }

    // 转换SysGoods到SysGoodsVO的辅助方法
    private SysGoodsVO convertToVO(SysGoods sysGoods) {
        SysGoodsVO sysGoodsVO = new SysGoodsVO();
        BeanUtil.copyProperties(sysGoods, sysGoodsVO);
        return sysGoodsVO;
    }
    private LambdaQueryWrapper<SysGoods> createWrapper(Map<String, Object> params) {
        String goodName = (String) params.get("goodName");
        String goodCategory = (String) params.get("goodCategory");
        String goodDescription = (String) params.get("goodDescription");
        String status = (String) params.get("status");

        LambdaQueryWrapper<SysGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(goodName), SysGoods::getGoodName, goodName);
        wrapper.like(StrUtil.isNotEmpty(goodCategory), SysGoods::getGoodCategory, goodCategory);
        wrapper.like(StrUtil.isNotEmpty(goodDescription), SysGoods::getGoodDescription, goodDescription);
        wrapper.eq(StrUtil.isNotEmpty(status), SysGoods::getStatus, status);

        return wrapper;
    }
}
